package com.itheima.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.*;
import com.itheima.service.DeptService;
import com.itheima.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.parsing.EmptyReaderEventListener;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 员工管理
 */
@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpController {

    @Autowired
    private EmpService empService;

    //    @GetMapping
//   public Result page(@RequestParam(defaultValue = "1") Integer page ,
//                @RequestParam(defaultValue = "10") Integer pageSize) {
//
//        PageResult pageResult = empService.page(page,pageSize);
//        return Result.success(pageResult);
//    }
    @Autowired
    private DeptService deptService;
    @GetMapping
    public Result page(EmpQueryParam param) {
        Page<Emp> page = new Page<>(param.getPage(),param.getPageSize());
        LambdaQueryWrapper<Emp> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(param.getName()),Emp::getName,param.getName());
        wrapper.eq(param.getGender()!=null,Emp::getGender,param.getGender());
        wrapper.between(param.getBegin()!=null&&param.getEnd()!=null,Emp::getCreateTime,param.getBegin(),param.getEnd());

        page = empService.page(page,wrapper);
        List<Emp> list = page.getRecords();
        list.forEach(emp -> {
            if(emp.getDeptId() != null) {
                Dept dept = deptService.getById(emp.getDeptId());
                if(dept != null) {
                    emp.setDeptName(dept.getName());
                }
            }
        });

        PageResult pageResult = new PageResult(page.getTotal(),page.getRecords());
        return Result.success(pageResult);
    }
    @PostMapping
    public Result save(@RequestBody Emp emp) throws Exception {
      //  log.info("请求参数emp: {}", emp);
        empService.saveEmpAndExprList(emp);
        return Result.success();
    }
    @DeleteMapping
    public Result delete(@RequestParam("ids") List<Integer> ids){
        log.info("批量删除部门: ids={} ", ids);
        empService.deleteEmpAndExprList(ids);
        return Result.success();
    }
    /**
     * 查询回显
     */
    @GetMapping("/{id}")
    public Result getInfo(@PathVariable Integer id){
        log.info("根据id查询员工的详细信息");
        Emp emp  = empService.getEmpAndExprListById(id);
        return Result.success(emp);
    }
    @GetMapping("/list")
    public Result listAll(){
        log.info("根据id查询员工的详细信息");
        List<Emp> list  = empService.list();
        return Result.success(list);
    }

    /**
     * 更新员工信息
     */
    @PutMapping
    public Result update(@RequestBody Emp emp){

        log.info("修改员工信息, {}", emp);
        empService.updateEmpAndExprList(emp);
        return Result.success();
    }
}